package com.specialized.coupon.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.specialized.common.core.domain.Result;
import com.specialized.coupon.domain.CouponSession;
import com.specialized.coupon.domain.Vouchers;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//import com.specialized.common.log.annotation.Log;
//import com.specialized.common.log.enums.BusinessType;
import com.specialized.common.security.annotation.RequiresPermissions;
import com.specialized.coupon.domain.Session;
import com.specialized.coupon.service.ISessionService;
import com.specialized.common.core.web.controller.BaseController;
import com.specialized.common.core.utils.poi.ExcelUtil;

/**
 * 抢券场次Controller
 *
 * @author djh
 * @date 2024-07-30
 */
@RestController
@RequestMapping("/Session")
public class SessionController extends BaseController {

    @Autowired
    private ISessionService sessionService;

    /**
     * 查询抢券场次列表
     */
    @RequiresPermissions("coupon:Session:list")
    @GetMapping("/list")
    public Result list(Session session) {
        startPage();
        List<Session> list = sessionService.selectSessionList(session);
        return getDataTable(list);
    }

    /**
     * 导出抢券场次列表
     */
    @RequiresPermissions("coupon:Session:export")
//    @Log(title = "抢券场次", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Session session) {
        List<Session> list = sessionService.selectSessionList(session);
        ExcelUtil<Session> util = new ExcelUtil<Session>(Session. class);
        util.exportExcel(response, list, "抢券场次数据");
    }

    /**
     * 获取抢券场次详细信息
     */
    @RequiresPermissions("coupon:Session:query")
    @GetMapping(value = "/{sessionId}")
    public Result getInfo(@PathVariable("sessionId") Long sessionId) {
        return success(sessionService.selectSessionBySessionId(sessionId));
    }

    /**
     * 新增抢券场次
     */
    @RequiresPermissions("coupon:Session:add")
//    @Log(title = "抢券场次", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody Session session) {
        return toAjax(sessionService.insertSession(session));
    }

    /**
     * 修改抢券场次
     */
    @RequiresPermissions("coupon:Session:edit")
//    @Log(title = "抢券场次", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody Session session) {
        return toAjax(sessionService.updateSession(session));
    }

    /**
     * 删除抢券场次
     */
    @RequiresPermissions("coupon:Session:remove")
//    @Log(title = "抢券场次", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sessionIds}")
    public Result remove(@PathVariable Long[] sessionIds) {
        return toAjax(sessionService.deleteSessionBySessionIds(sessionIds));
    }


    /**
     * 代金券
     */
    @PostMapping("/findSessionCoupon")
    public Result<List<Vouchers>> findSessionCoupon() {
        List<Vouchers> vouchersList = sessionService.findSessionCoupon();
        return Result.success(vouchersList);
    }




    /**
     * 幂等
     */
    @GetMapping("/gettoken")
    public Result getToken() {
        Result token = sessionService.getToken();
        return token;
    }

    /**
     * 优惠券秒杀
     * 优惠券与用户表 记录用户有哪张优惠券
     * 优惠券与场次关系表 扣减库存-1
     */
    @PostMapping("/SpikesCoupons")
    public Result SpikesCoupons(@RequestBody CouponSession couponSession) {
        Result updedSpikesCoupons = sessionService.updSpikesCoupons(couponSession);
        return updedSpikesCoupons;
    }







}
